//
//  AddBankView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/8.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol AddBankViewDelegate {
    @objc optional func selectType(index:Int) //选择操作
}

class AddBankView: UIView {
    
    weak var delegate : AddBankViewDelegate?
    
    fileprivate let font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))

    fileprivate let numberView = UIView()
    fileprivate let bankView = UIView()
    fileprivate let cardView = UIView()
    fileprivate let nameView = UIView()
    
    fileprivate let bankButton = UIButton()
    fileprivate let cardButton = UIButton()
    
    let numberField = UITextField()
    let nameField = UITextField()
    
    var bankText = BankofChina {
        didSet{
            bankButton.setTitle(bankText, for: .normal)
        }
    }
    
    var cardText = creditCard_title {
        didSet{
            cardButton.setTitle(cardText, for: .normal)
        }
    }
    
    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.verifyColor()
        
        numberView.backgroundColor = UIColor.white
        self.addSubview(numberView)
        
        bankView.backgroundColor = UIColor.white
        self.addSubview(bankView)
        
        cardView.backgroundColor = UIColor.white
        self.addSubview(cardView)
        
        nameView.backgroundColor = UIColor.white
        self.addSubview(nameView)
        
        let numberTitleLabel = UILabel()
        numberTitleLabel.text = bankCardNumber
        numberTitleLabel.font = font
        numberView.addSubview(numberTitleLabel)
        
        numberField.keyboardType = .numberPad
        numberField.placeholder = "8888 8888 8888 8888"
        numberField.textAlignment = .right
        numberField.font = font
        numberView.addSubview(numberField)
        
        let photoBtn = UIButton()
        photoBtn.layer.contents = #imageLiteral(resourceName: "camera").cgImage
        photoBtn.tag = 4
        photoBtn.addTarget(self, action: #selector(selectOperte(sender:)), for: .touchUpInside)
        numberView.addSubview(photoBtn)
        
        let bankTitleLabel = UILabel()
        bankTitleLabel.text = bankOf
        bankTitleLabel.font = font
        bankView.addSubview(bankTitleLabel)
        
        bankButton.setTitle(BankofChina, for: .normal)
        bankButton.setTitleColor(UIColor.black, for: .normal)
        bankButton.titleLabel?.font = font
        bankButton.tag = 1
        bankButton.addTarget(self, action: #selector(selectOperte(sender:)), for: .touchUpInside)
        bankView.addSubview(bankButton)
        
        let cardTitleLabel = UILabel()
        cardTitleLabel.text = currency
        cardTitleLabel.font = font
        cardView.addSubview(cardTitleLabel)
        
        cardButton.setTitle(creditCard, for: .normal)
        cardButton.setTitleColor(UIColor.black, for: .normal)
        cardButton.titleLabel?.font = font
        cardButton.tag = 2
        cardButton.addTarget(self, action: #selector(selectOperte(sender:)), for: .touchUpInside)
        cardView.addSubview(cardButton)
        
        let nameTitleLabel = UILabel()
        nameTitleLabel.text = houseHolder
        nameTitleLabel.font = font
        nameView.addSubview(nameTitleLabel)
        
        nameField.placeholder = nameTips
        nameField.font = font
        nameField.textAlignment = .right
        nameView.addSubview(nameField)
        
        let addButton = UIButton()
        addButton.layer.cornerRadius = RATIO_H(maxNum: 20)
        addButton.layer.masksToBounds = true
        addButton.backgroundColor = UIColor.red
        addButton.setTitle(add_title, for: .normal)
        addButton.titleLabel?.font = font
        addButton.tag = 3
        addButton.addTarget(self, action: #selector(selectOperte(sender:)), for: .touchUpInside)
        self.addSubview(addButton)
        
        numberView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 30))
            make.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 50))
        }
        
        bankView.snp.makeConstraints { (make) in
            make.top.equalTo(numberView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(numberView)
        }
        
        cardView.snp.makeConstraints { (make) in
            make.top.equalTo(bankView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(bankView)
        }
        
        nameView.snp.makeConstraints { (make) in
            make.top.equalTo(cardView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(cardView)
        }
        
        addButton.snp.makeConstraints { (make) in
            make.top.equalTo(nameView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        numberTitleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        photoBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -5))
            make.centerY.equalToSuperview()
            make.width.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        numberField.snp.makeConstraints { (make) in
        make.left.equalTo(numberTitleLabel.snp.right).offset(RATIO(maxNum: 10))
            make.right.equalTo(photoBtn.snp.left).offset(RATIO(maxNum: -10))
            make.centerY.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 30))
            
        }
        
        bankTitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(numberTitleLabel)
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }

        bankButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }

        cardTitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bankTitleLabel)
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }

        cardButton.snp.makeConstraints { (make) in
            make.right.equalTo(bankButton)
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }

        nameTitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(cardTitleLabel)
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }

        nameField.snp.makeConstraints { (make) in
            make.right.equalTo(cardButton)
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(numberField)
        }
    }
    
    //MARK: 选择操作
    @objc fileprivate func selectOperte(sender:UIButton) {
        
        self.delegate?.selectType!(index: sender.tag)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
